﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002A3 RID: 675
	internal class FinallyObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000ED6 RID: 3798 RVA: 0x0004568C File Offset: 0x00043A8C
		public FinallyObservable(IObservable<T> source, Action finallyAction) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.finallyAction = finallyAction;
		}

		// Token: 0x06000ED7 RID: 3799 RVA: 0x000456A8 File Offset: 0x00043AA8
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new FinallyObservable<T>.Finally(this, observer, cancel).Run();
		}

		// Token: 0x04000895 RID: 2197
		private readonly IObservable<T> source;

		// Token: 0x04000896 RID: 2198
		private readonly Action finallyAction;

		// Token: 0x020002A4 RID: 676
		private class Finally : OperatorObserverBase<T, T>
		{
			// Token: 0x06000ED8 RID: 3800 RVA: 0x000456B7 File Offset: 0x00043AB7
			public Finally(FinallyObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000ED9 RID: 3801 RVA: 0x000456C8 File Offset: 0x00043AC8
			public IDisposable Run()
			{
				IDisposable disposable;
				try
				{
					disposable = this.parent.source.Subscribe(this);
				}
				catch
				{
					this.parent.finallyAction();
					throw;
				}
				return StableCompositeDisposable.Create(disposable, Disposable.Create(delegate
				{
					this.parent.finallyAction();
				}));
			}

			// Token: 0x06000EDA RID: 3802 RVA: 0x00045728 File Offset: 0x00043B28
			public override void OnNext(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000EDB RID: 3803 RVA: 0x00045738 File Offset: 0x00043B38
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000EDC RID: 3804 RVA: 0x00045770 File Offset: 0x00043B70
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000897 RID: 2199
			private readonly FinallyObservable<T> parent;
		}
	}
}
